<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://genshi.edgewall.org/">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Mail Throttler</title>
    <link href="default.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  </head>
  <body>
    <div id="header">
      <h1>Mail Throttler</h1>
      <h2>Admin Console</h2>
    </div>
    <div id="menu">
      <ul>
        <li><a href="/">Home</a></li>
        <li><a href="#">Contact Us</a></li>
      </ul>
    </div>
    <div id="content">
      <div>
        <h2>Current Stats</h2>

        <ul>
	  <li>Server has been up for: <span id="uptime">Updating...</span></li>
          <li>Currently counting <span id="currentCount">0</span> messages</li>
          <li>Seen a total of <span id="totalCount">0</span> messages</li>
          <li id="additionalCounts">Additional Counts<br/>
          <ul><li>Calculating ...</li></ul>
          </li>
          <span id="extraCounts"></span>
        </ul>   
        <h2>Actions</h2>
        <ul>
	  <li> <a id="refreshCounts" href="#">Refresh Counts</a> </li>
          <li>
            <form action="/" method="post">
              <div>Change Logging level to: 
              <select name="logLevel">
                <option>DEBUG</option>
                <option>INFO</option>
              </select>
              <input type="submit" value="Change" />
              </div>
            </form>
          </li>
          <li><a href="/config">View Current Config</a></li>
          <li><a href="#" id="clearQueuesLink">Force Clear the Queues</a><span id="clearQueuesMsg"></span></li>
	  <li><a id="refreshLog" href="#">Refresh Log</a></li>
	  <li><a id="refreshUptime" href="#">Refresh Uptime</a></li>	  
        </ul>
        <h2>Current Log - ${currentLevel}</h2>
        <textarea id="logbox" readonly="readonly" ></textarea>
        </div>
    </div>
    <div id="footer">
      <p>Copyright &copy; 2009-10 Andrew Cox. Designed by <a href="http://www.freecsstemplates.org" class="link1">Free CSS Templates</a></p>
    </div>
    <script type="text/javascript">
      //<![CDATA[

      function refreshLog()
      {
      //Make AJAX call here
        $.ajax(
        { type:"GET",
          url:"ajax?action=log",
          dataType:"json",
          success: function (data) 
                   {
                   $("#logbox").val("")            
                   for (i = data.log.length-1; i > -1 ;i--)
                     {
                     logitem = data.log[i];
                     var line = logitem.asctime + " - " + logitem.name + " - " + logitem.levelname + " - " + logitem.message + "\n";
                     $("#logbox").val(line + $("#logbox").val());
                     }
                   }
        }
        )
      }

      function clearQueues()
      {
      $("#clearQueuesMsg").hide()
         $.ajax(
        { type:"GET",
          url:"forceclear",
          dataType:"text",
          success: function (data)
                   {
                   $("#clearQueuesMsg").text("Queues Cleared").fadeIn(1000, 
                      function ( )
                      {
                      $("#clearQueuesMsg").fadeOut(1000);
                      }
                    );             
                   }
          });
      }

      function refreshCounts()
      {
      //Make AJAX call here
        $.ajax(
        { type:"GET",
          url:"ajax?action=counts",
          dataType:"json",
          success: function (data) 
                   {
                   var showAdditional = false
                   $("#additionalCounts ul").empty()
                   $("#extraCounts").text("")
                   for ( var key in data.counts )
                     {
                     if (key == "current")
                       { $("#currentCount").text( data.counts.current ); }
                     else if (key == "total")
                       { $("#totalCount").text( data.counts.total ); }
                     else 
                       {
                       var encodedKey = key.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;')
                       var info = encodedKey + ": " + data.counts[key].current.toString() + "," + data.counts[key].total.toString();
                       $("#additionalCounts ul").append( "<li>" + info + "</li>" );
                       showAdditional = true;
                       }
                     }               
                   $("#additionalCounts").toggle(showAdditional);
                   }
        }
        )
      }

      function refreshUptime()
      {
        $.ajax(
        { type:"GET",
          url:"ajax?action=uptime",
          dataType:"json",
          success: function (data) 
                   {
		   var d = data.uptime_d; //Whole days;
		   var h = Math.floor(data.uptime_s / 3600); //Turn seconds into whole hours
		   var s = data.uptime_s % 3600; //Take the left over
		   var m = Math.floor(s / 60); //Turn left over seconds into whole minutes
		   s = (s - (m*60)) % 60; //Take the left over
		   var outText = d + " days " + h + " hours " + m + " minutes " + s + " seconds";
		   $("#uptime").text( outText );
                   }
        }
	)
      }

      $(document).ready( function () {
      refreshLog();
      refreshCounts();
      refreshUptime();
      $("#refreshUptime").click(refreshUptime);
      $("#refreshLog").click(refreshLog);
      $("#refreshCounts").click(refreshCounts);
      $("#clearQueuesLink").click(clearQueues);
      setInterval(refreshLog, 50000);
      setInterval(refreshCounts, 500);
      setInterval(refreshUptime, 50000);
      }
      );
      //]]>
    </script>
  </body>
</html>
